package com.berry.MedCheck;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.berry.MedCheck.db.UserDB;

/**
 * Created with IntelliJ IDEA.
 * User: Airay.Rong
 * Date: 13-12-4
 * Time: 下午5:06
 * To change this template use File | Settings | File Templates.
 */
public class ClearAllRecordActivity extends Activity implements View.OnClickListener {
	private  Button mBtnAll;
	private Button mBtnCancel;
	private ProgressDialog progressDialog = null;

	private static final String[] PROJECTION = new String[]{
			UserDB.DBColumns._ID, // 0
			UserDB.DBColumns.USERID, // 2
			UserDB.DBColumns.DATE, // 3
			UserDB.DBColumns.SPO2, // 4
			UserDB.DBColumns.PR, // 5
			UserDB.DBColumns.PI, // 6
	};
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		setContentView(R.layout.clear_all_record_activity);
		mBtnAll = (Button) findViewById(R.id.btn_all);
		mBtnAll.setOnClickListener(this);
		mBtnCancel = (Button) findViewById(R.id.btn_cancel);
		mBtnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mBtnAll == v) {
			clearAllRecord();
		}
		if (mBtnCancel == v) {
			finish();
		}

	}

	private void clearAllRecord() {
		SharedPreferences sp = getSharedPreferences(Utils.PREFERENCE_NAME, 0);
		Long mCurrentUserID = sp.getLong(Utils.PRE_KEY_CURRENT_USER_ID, 0);
		if (mCurrentUserID < 0) {
			return;
		}
		String selection = "userid=" + mCurrentUserID;
		getContentResolver().delete(UserDB.DBColumns.MEASURE_URI, selection, null);
		showMessageDialog(R.string.done);
	}
	private void showMessageDialog(int strID) {
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setCanceledOnTouchOutside(false);
		alert.show();
		Window window = alert.getWindow();
		window.setContentView(R.layout.my_alert_dialog);
		TextView tv = (TextView) window.findViewById(R.id.message);
		tv.setText(this.getResources().getText(strID));
		Button btnCancel = (Button) window.findViewById(R.id.btn_cancel);
		btnCancel.setVisibility(View.GONE);
		Button btnOK = (Button) window.findViewById(R.id.btn_ok);
		btnOK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
}